package com.ltu.timkor6.gamesite.users.client;

import com.google.gwt.user.client.ui.Composite;
import com.ltu.timkor6.gamesite.client.GameSite;
import com.ltu.timkor6.gamesite.shared.UserInfo;

/**
 * Users
 * 
 * @author tim
 *
 */

public class Users
{
	private UserInfo userInfo = null;
	private GameSite gameSite = null;
	private SignIn signIn = null;
	private Profile profile = null;
	
	public Users(GameSite gameSite)
	{
		this.gameSite = gameSite;
		this.signIn = new SignIn(this);
	}
	
	public Composite getRegisterComposite()
	{
		return new Register(this).getComposite();
	}
	
	public Composite getSignInComposite()
	{
		return this.signIn.getComposite();
	}
	
	public Composite getProfileComposite()
	{
		this.profile = new Profile(this);
		
		return this.profile.getComposite();
	}
	
	public Composite getForgottenComposite()
	{
		return new Forgotten(this).getComposite();
	}
	
	public void showSignIn()
	{
		this.gameSite.moduleSignIn();
	}
	
	/**
	 * Gets the userInfo from the server.
	 *  - When successfull, return to update the layout.
	 */
	public void checkIfLoggedIn()
	{
		this.signIn.checkIfLoggedIn(SignInTypes.CHECK);
	}
	
	/**
	 * Return for checkUserIfLoggedIn()
	 * @param userInfo
	 */
	public void checkIfLoggedIn(UserInfo userInfo)
	{
		this.userInfo = userInfo;
		this.gameSite.loadModules();
	}
	
	/**
	 * Gets the userInfo from the server.
	 *  - When successfull, return to set the userInfo.
	 */
	public void checkUserInfo()
	{
		this.signIn.checkIfLoggedIn(SignInTypes.GET);
	}
	
	/**
	 * Gets the userInfo from the server.
	 *  - When successfull, return to set the userInfo.
	 */
	public void updatedUserInfo()
	{
		this.signIn.checkIfLoggedIn(SignInTypes.UPDATED);
	}
	
	/**
	 * Return for checkUserInfo()
	 * @param userInfo
	 */
	public void checkUserInfo(UserInfo userInfo)
	{
		this.userInfo = userInfo;
		
		this.gameSite.changeSignedIn();
	}
	
	/**
	 * Updates the session on the server side.
	 *  - When successfull, do nothing.
	 */
	public void checkUserSession()
	{
		this.signIn.checkIfLoggedIn(SignInTypes.SESSION);
	}
	
	/**
	 * Return the userInfo variable.
	 * @return
	 */
	public UserInfo getUserInfo()
	{
		return this.userInfo;
	}
	
	/**
	 * Return the userInfo variable.
	 * @return
	 */
	private void setUserInfo(UserInfo userInfo)
	{
		this.userInfo = userInfo;
	}
	
	/**
	 * Send sign out to the server.
	 */
	public void sendSignOut()
	{
		new SignOut(this).sendSignOut();
	}

	public void updateProfile(UserInfo userInfo)
	{
		setUserInfo(userInfo);
		this.profile.drawAgain();
	}
}
